export function addTab(routeName, params, title = null) {
  if (window.parent) {
    window.parent.postMessage({
      cmd: 'addTabItem',
      params: { name: routeName, title, params: this.$Base64.encode(encodeURIComponent(JSON.stringify(params))) }
    })
  } else {
    this.$EventBus.$emit('addTabItem', { name: routeName, title, params: this.$Base64.encode(encodeURIComponent(JSON.stringify(params))) })
  }
}

export function updateCurrentTabLabel(title) {
  if (window.parent) {
    window.parent.postMessage({
      cmd: 'updateCurrentTabLabel',
      params: { title: title }
    })
  } else {
    this.$EventBus.$emit('updateCurrentTabLabel', { title: title })
  }
}

export function addPageDataNeedReload(pageName) {
  if (window.parent) {
    window.parent.postMessage({
      cmd: 'addPageDataNeedReload',
      params: { pageName: pageName }
    })
  } else {
    this.$EventBus.$emit('addPageDataNeedReload', { pageName: pageName })
  }
}

export function getPageDataNeedReload(pageName) {
  if (window.parent) {
    window.parent.postMessage({
      cmd: 'getPageDataNeedReload',
      params: { pageName: pageName }
    })
  } else {
    this.$EventBus.$emit('getPageDataNeedReload', { pageName: pageName })
  }
}

export function getQueryVariable(variable) {
  // const url = window.location.href
  // console.log('url', url, window.location)
  // var query = url.substr(url.indexOf('?') + 1, url.length)
  // var vars = query.split('&')
  // for (let i = 0; i < vars.length; i++) {
  //   var pair = vars[i].split('=')
  //   if (pair[0] === variable) { return pair[1] }
  // }
  // return (false)
}
